package com.sqkj.controller;



import com.sqkj.util.CommonUtil;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class loginController {
    public static final  String wxAppid = "wx3ecbee92746ba055";
    public static final  String wxSecret = "9cee742d86e0cad04ebb8b874b7d2b7c";
    public static final  String grant_type = "authorization_code";
    public static final  String url = "https://api.weixin.qq.com/sns/jscode2session?";
    @GetMapping("/login_weixin")
    public Map decodeUserInfo(String encryptedData, String iv, String code) {
        Map map = new HashMap();


        //登录凭证不能为空
        if (code == null || code.length() == 0) { map.put("status", 0);
            map.put("msg", "code 不能为空");
            return map;
        }


         String params = "appid=" + wxAppid + "&secret=" + wxSecret + "&js_code=" + code + "&grant_type=" + grant_type;

        JSONObject json = CommonUtil.httpsRequest(url + params, "GET", " ");

        System.out.println(json);
        //获取会话密钥（session_key）
        String session_key = json.get("session_key").toString();
        //用户的唯一标识（openid）
        String openid =  json.get("openid").toString();

        map.put("openid", openid);



        return map;


    }
}
